package com.ag.hmk.action;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;


import com.ag.bean.TbClassiccase;
import com.ag.bean.TbLaw;
import com.ag.bean.TbUri;
import com.ag.common.AnnexPath;
import com.ag.hmk.service.impl.CaseServImpl;
import com.ag.hmk.service.impl.LawServImpl;
import com.ag.lzm.dao.TbUriDAO;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 
 * @author hmk
 * 实现文件的下载，
 */
public class DownLoadAction extends ActionSupport {
	private static final long serialVersionUID = 878951L;
	
	@Resource(name = "LawServImpl")
	private LawServImpl lawServImpl;
	
	@Resource(name = "CaseServImpl")
	private CaseServImpl caseServImpl;
	
	@Resource(name = "LZMTbUriDAO")
	private TbUriDAO uriDAO;
	
	private String lawID;  //法规ID
	private String caseID; //案例ID
	private int findtype;  //区分下载法规还是案例
	

	private String fileName; // 文件名
	private String fileURL; // 要下载的文件路径
	private String realFileName; //附件原名
	
	 public String execute() {  
		try {
			switch (findtype) {
			case 0: //下载法规
				TbLaw law = lawServImpl.findByID(lawID);
				if(law!=null){
					System.out.println("===法规的文件名：==="+law.getLawFjName());
					TbUri uri0= uriDAO.findById(AnnexPath.LAW_ANNEX_PATH);
					fileURL = uri0.getUPath(); 
					fileName = law.getLawFjName() ;
					realFileName = 	law.getLawFjRote();
				}else{
					System.out.println("=====为空=");
					fileName="";
					realFileName="";
				}
				break;
			case 1: //下载案例
				TbClassiccase mycase = caseServImpl.findByID(caseID);
				if (mycase!=null) {
					System.out.println("====案例的文件名：==="+mycase.getCcFjName());
					TbUri uri1= uriDAO.findById(AnnexPath.CASE_ANNEX_PATH);
					fileURL = uri1.getUPath();
					fileName = mycase.getCcFjName();
					realFileName = mycase.getCcFjRote();
				} else {
					fileName="";
					realFileName="";
				}
				break;

			default:
				fileName="";
				realFileName="";
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
			fileName="";
			realFileName="";
		}	
	        return SUCCESS;   
	    }

	// 从下载文件原始存放路径读取得到文件输出流
	public InputStream getDownloadFile() throws FileNotFoundException {
		if (fileName!=null && fileName.length()>0) {
			return new FileInputStream(fileURL+fileName);
		} else {
			return null;
		}
		
	}

	// 如果下载文件名为中文，进行字符编码转换
	public String getDownloadChineseFileName() {
		String downloadChineseFileName = realFileName;
		try {
			downloadChineseFileName = new String(downloadChineseFileName
					.getBytes(), "ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return downloadChineseFileName;
	}


	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public String getFileURL() {
		return fileURL;
	}

	public void setFileURL(String fileURL) {
		this.fileURL = fileURL;
	}

	public void setLawServImpl(LawServImpl lawServImpl) {
		this.lawServImpl = lawServImpl;
	}

	public void setCaseServImpl(CaseServImpl caseServImpl) {
		this.caseServImpl = caseServImpl;
	}

	public void setLawID(String lawID) {
		this.lawID = lawID;
	}

	public void setCaseID(String caseID) {
		this.caseID = caseID;
	}

	public void setFindtype(int findtype) {
		this.findtype = findtype;
	}
}
